package com.kantboot.api.lbs.service.impl;

import com.kantboot.api.lbs.domain.entity.ApiLbsAd;
import com.kantboot.api.lbs.domain.entity.ApiLbsArea;
import com.kantboot.api.lbs.repository.ApiLbsAreaRepository;
import com.kantboot.api.lbs.service.IApiLbsAdService;
import com.kantboot.api.lbs.service.IApiLbsAreaService;
import com.kantboot.api.lbs.util.TencentLbsUtil;
import com.kantboot.system.setting.service.ISysSettingService;
import com.kantboot.util.common.http.HttpRequestHeaderUtil;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 获取地理位置
 */
@Service
public class ApiLbsAreaServiceImpl implements IApiLbsAreaService {

    @Resource
    private ApiLbsAreaRepository repository;


    @Resource
    private ISysSettingService settingService;

    @Resource
    private HttpRequestHeaderUtil httpRequestHeaderUtil;

    @Resource
    private IApiLbsAdService lbsAdService;

    @Override
    public ApiLbsArea getByAdcode(String adcode) {
        return repository.findByAdcode(adcode);
    }

    @Cacheable(value = "ApiLbsArea", key = "#root.methodName")
    @Override
    public List<ApiLbsArea> getAll() {
        return repository.findAll();
    }

    @Override
    public ApiLbsArea getByIp(String ip) {
        String tencentMapApiKey = settingService.getValue("lbs", "tencentMapApiKey");
        ApiLbsAd lbsVO = TencentLbsUtil.byIp(ip, tencentMapApiKey);
        return repository.findByAdcode(lbsVO.getAdcode());
    }

    @Override
    public ApiLbsArea getBySelfIp() {
        return getByIp(httpRequestHeaderUtil.getIp());
    }


    @Override
    public ApiLbsArea getByLngAndLat(String lng, String lat) {
        ApiLbsAd lbs = lbsAdService.getByLngAndLat(lng, lat);
        return repository.findByAdcode(lbs.getAdcode());
    }
}
